package com.rs2lite.utils;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.rs2lite.RS2Lite;

/**
 * Class AutoTyperGUI, This class is the input class for the autotyper
 * 
 * Note: This class is currently in development, and will be changed.
 * 
 * @author Nicole <nicole@rune-server.org> This file is protected by The BSD
 *         License, You should have recieved a copy named "BSD License.txt"
 */

public class AutoTyperGUI {

	/**
	 * Field declaration
	 */
	private JDialog frame;
	private JList texts;
	private DefaultListModel model;

	/**
	 * Construct the GUI for the autotyper
	 */
	public AutoTyperGUI() {
		frame = new JDialog(RS2Lite.frame, "RS2Lite - Auto Typer");
		texts = new JList();
		model = new DefaultListModel();
		for (String s : RS2Lite.getAutoTyper().getTexts()) {
			if (s != null)
				model.addElement(s);
		}
		texts.setModel(model);
		JScrollPane pane = new JScrollPane();
		pane.setViewportView(texts);
		frame.add(pane, BorderLayout.CENTER);
		frame.setSize(500, 300);
		frame.setVisible(true);
		JPanel panel = new JPanel();
		JButton add = new JButton("Add text");
		add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String s = JOptionPane.showInputDialog(frame,
						"Enter a string to say");
				if (s != null) {
					add(s);
				}
			}
		});
		panel.add(add);
		JButton remove = new JButton("Remove");
		remove.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (texts.getSelectedIndex() != -1) {
					model.remove(texts.getSelectedIndex());
				}
			}
		});
		panel.add(remove);
		final JLabel currentDelay = new JLabel();
		currentDelay.setText("Current delay: "
				+ RS2Lite.getAutoTyper().getDelay() + "ms");
		JButton delay = new JButton("Set delay");
		delay.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String delay = JOptionPane
						.showInputDialog(frame,
								"Enter the new delay in milliseconds\n(1000ms to a second)");
				try {
					long newDelay = Long.parseLong(delay);
					if (newDelay < 0)
						throw new Exception("Delay < 0!");
					RS2Lite.getAutoTyper().setDelay(newDelay);
					currentDelay.setText("Current delay: "
							+ RS2Lite.getAutoTyper().getDelay() + "ms");
				} catch (Exception e) {
					JOptionPane.showMessageDialog(frame,
							"Delay must be a number, and also greater than 0!",
							"ERROR!", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		panel.add(delay);
		JButton start = new JButton("Start");
		start.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				frame.setVisible(false);
				frame.dispose();
				String[] strings = new String[model.getSize()];
				Object[] objects = model.toArray();
				for (int i = 0; i < objects.length; i++) {
					strings[i] = objects[i].toString();
				}
				if (strings.length == 0)
					JOptionPane.showMessageDialog(frame,
							"You did not input any text to say!", "ERROR!",
							JOptionPane.ERROR_MESSAGE);
				else
					RS2Lite.getAutoTyper().start(strings);
			}
		});
		panel.add(start);
		panel.add(currentDelay);
		frame.add(panel, BorderLayout.SOUTH);

		frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}

	public void add(String s) {
		model.addElement(s);
	}
}
